package map;


import com.sun.javafx.collections.MappingChange;
import org.apache.commons.math3.linear.CholeskyDecomposition;

import java.util.*;

public class Map_ {

    public static void main(String[] args) {
        Map<String, Object> map = new HashMap<>();
        map.put("ui", new Book("", 100));
        map.put("ui", "java1");
        map.put("鹿晗", "java2");
        map.put("胡歌", "java3");
        map.put("小星", null);
        map.put(null, "鞠维强");
//常用方法
//        System.out.println("map=" + map);
//        //根据键删除
//        map.remove(null);
//        //根据键获取值
//        Object val = map.get("小星");
//        System.out.println(val);
//        //获取元素个数
//        System.out.println("k-v:" + map.size());
//        //清空k-v
////        map.clear();
////        System.out.println("map:" + map);
//        //查找键是否存在
//        System.out.println(map.containsKey("hashmap"));//False
//        //获取所有的键
//        System.out.println("所有的键："+map.keySet());
//        //获取所有的值
//        System.out.println("所有的值："+map.values());

        /**
         * 遍历方式 第一组：先取出所有的kye,通过key取出对应的value
         */
        Set<String> keySet = map.keySet();
        //(1)增强for
        for (String key : keySet) {
            System.out.println(key + "_" + map.get(key));
        }
        //(2)迭代器
        System.out.println("第二种方法-------------");
        Iterator<String> iterator = keySet.iterator();
        while (iterator.hasNext()) {
            String key = iterator.next();
            System.out.println(key + "_" + map.get(key));
        }


        /**
         * 遍历方式 第二组：把所有的value值取出来
         */
        System.out.println("第二组*******************************-------------");
        Collection<Object> values = map.values();
        for (Object value : values) {
            System.out.println(value);
        }
        //(2)迭代器
        System.out.println("第二种方法-------------");
        Iterator<Object> iterator1 = values.iterator();
        while (iterator1.hasNext()) {
            Object key = iterator1.next();
            System.out.println(key);
        }
        System.out.println("第三组*******************************-------------");
        //通过EntrySet 来获取k-v
        Set<Map.Entry<String, Object>> entrySet = map.entrySet();//EntrySet<Map.Entry<k,v>>
        for (Object entry : entrySet) {
            //将entry转换为 Map.Entry
            Map.Entry m = (Map.Entry) entry;
            System.out.println(m.getKey() + "_" + m.getValue());
        }
        //迭代器
        System.out.println("第二种方法-------------");
        Iterator<Map.Entry<String, Object>> iterator2 = entrySet.iterator();
        while (iterator2.hasNext()) {
            Object entry = iterator2.next();
//            System.out.println(key.getClass());
            //向下转型 Map.Entry
            Map.Entry m = (Map.Entry) entry;
            System.out.println(m.getKey() + "_" + m.getValue());
        }

    }
}


class Book {
    private String name;
    private int num;

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", num=" + num +
                '}';
    }

    public Book(String name, int num) {
        this.name = name;
        this.num = num;
    }
}
